package com.mm.dss.demo;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.google.gson.Gson;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mm.dss.demo.Interface.OkGoUtilsInterFace;
import com.mm.dss.demo.base.BaseActivity;
import com.mm.dss.demo.tools.Constances;
import com.mm.dss.demo.tools.OkgoUtils;
import com.mm.dss.demo.tools.SharedPreferencedUtils;
import com.mm.dss.demo.tools.Utils;
import com.mm.dss.demo.tools.WebServiceUtils;
import com.mm.dss.demo.tools.WeiboDialogUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.serialization.SoapObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/*
* 去完成群防群测的任务
* */
public class ToqfqcActivity extends BaseActivity {
    RelativeLayout layout_back;
    TextView txt_title;
    private int IMAGE_PICKER=0;
    private Button btn;//
    private EditText edit_txt;//输入框
    private TextView imgs;//图片张数
    ArrayList<ImageItem> images;
    private Dialog mWeiboDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toqfqc);
        txt_title= (TextView) findViewById(R.id.txt_title);
        layout_back= (RelativeLayout) findViewById(R.id.layout_back);
        txt_title.setText("完成任务");
        btn= (Button) findViewById(R.id.btn);
        edit_txt= (EditText) findViewById(R.id.edit_txt);
        imgs= (TextView) findViewById(R.id.imgs);
        layout_back.setVisibility(View.VISIBLE);
        initdata();
    }

    protected void initdata() {


    }
    public void onclick(View view){
        switch (view.getId()){
            case R.id.btn:
                //网络请求，先判断
                if (edit_txt.getText().toString().equals("")){
                    Toast.makeText(ToqfqcActivity.this,"请先输入，任务完成情况",Toast.LENGTH_LONG).show();
                }else {
                    mWeiboDialog = WeiboDialogUtils.
                            createLoadingDialog(ToqfqcActivity.this,
                                    "加载中...");
                    List<String> imglist=new ArrayList<>();
                    if (images!=null&&images.size()>0){
                        for (int i=0;i<images.size();i++){
                            Bitmap bitmap = Utils.compressBitmap(images.get(i).path, 600, 1024, 30);
                            imglist.add(Utils.bitmapToBase64(bitmap));
                        }
                        Log.i("上报File文件",""+images.get(0).path);
                    }
                    Map<String,Object> map=new HashMap<>();
                    map.put("ID",getIntent().getStringExtra("rwid"));
                    map.put("XCQK",edit_txt.getText().toString());
                    map.put("IMAGE",imglist);
                    Gson gson = new Gson();
                    String jsonText = gson.toJson(map);
                    Log.i("json转换的数据", jsonText);
                    Map<String, String> jsonmap = new HashMap<>();
                    jsonmap.put("jsonText", jsonText);
                    WebServiceUtils.callWebService(Constances.webhosturl, "N06_QCQFUp", jsonmap, new WebServiceUtils.WebServiceCallBack() {
                        @Override
                        public void callBack(SoapObject result) {
                            WeiboDialogUtils.closeDialog(mWeiboDialog);
                            Log.i("上报成功1", result.getProperty(0).toString());
                            QfqcInfoActivity.activity.finish();
                            finish();
                        }
                    });
                }

                break;
            case R.id.layout_back:
                finish();
                break;
            case R.id.btnimg:
                //图片选择
                Intent intent = new Intent(this, ImageGridActivity.class);
                startActivityForResult(intent, IMAGE_PICKER);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == IMAGE_PICKER) {
                images= (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                Log.i("图片选择",images.get(0).path+"刘习亭");
                imgs.setText(images.size()+"张");
              //  MyAdapter adapter = new MyAdapter(images);
               // gridView.setAdapter(adapter);
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
